package com.wxy.gks.springbootprojectday22practice.web.controller;

import com.wxy.gks.springbootprojectday22practice.bean.User;
import com.wxy.gks.springbootprojectday22practice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {


        return "login";
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (user != null && !user.getName().equals("")) {
            User userByUsername = userService.findUserByUsername(user.getName());
            if (userByUsername == null) {
                session.setAttribute("msessage", "用户名不正确!");
            } else {
                User userByNameAndPass = userService.findUserByNameAndPass(user);
                if (userByNameAndPass == null) {
                    session.setAttribute("message", "密码错误!");
                return "redirect:toLogin";
                } else {
                    System.out.println("查询到的结果" + user);
                    session.setAttribute("loginUser",user);
//                    return "forward:/employee/findAll" ;
                    return "redirect:/employee/findAll";
                }
            }
        } else {
            session.setAttribute("message", "用户名称不能为空!");
            return "redirect:toLogin";
        }
        return "";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
//        session.removeAttribute("loginUser");
//        session.removeAttribute("msg");
//        session.removeAttribute("qq_pic");

        return "redirect:toLogin";
    }


}
